package com.tyqx.hashchainbiz;

// import com.alibaba.fastjson.JSON;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tyqx.hashchainbean.module.*;
import com.tyqx.hashchainbean.test.BaiWanTest;
import com.tyqx.hashchainbiz.service.MysqlDataBaseAndMongDbConfigService;
import com.tyqx.hashchainbiz.service.NoHashValueJsonService;
import hashchainutil.common.EmptyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class SchedulerSuccessBatch {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    @Resource
    private NoHashValueJsonService noHashValueJsonService;


    @Resource
    private MysqlDataBaseAndMongDbConfigService mysqlDataBaseAndMongDbConfigService;

    ///@Async
    //表示每隔秒
//    @Scheduled(fixedRate = 60 * 1000 * 10)
    @Scheduled(fixedDelay = 1000 * 1)
    public void testTasks() throws Exception {
        System.out.println("迁移上链信息定时任务执行时间：" + dateFormat.format(new Date()));
        Integer status = 1;
        Integer batchSize=1000;
        //List<NoHashValueJson> noHashValueJsonList = noHashValueJsonService.selectByStatus(status);

        List<NoHashValueJson> noHashValueJsonList = noHashValueJsonService.selectByStatusBatch(status,batchSize);
        List<HashIndex> hashIndexList=new ArrayList<>();
        List<JZTLogistics> jztLogisticsList=new ArrayList<>();
        List<String> noHashValueJsonHashValueList=new ArrayList<>();
        List<JZTAttachmentBusiness> jztAttachmentBusinessList=new ArrayList<>();
        List<JZTCodeCirculation> jztCodeCirculationList=new ArrayList<>();
        List<JZTCodeCirculationAttachment> jztCodeCirculationAttachmentList=new ArrayList<>();
        List<JZTGS1Str> jztgs1List=new ArrayList<>();
        List<JZTGS1Attachment> jztgs1AttachmentList=new ArrayList<>();
        List<BaiWanTest> baiWanTestList=new ArrayList<>();
        List<WxAccountFirmVerification> wxAccountFirmVerificationList=new ArrayList<>();
        if (EmptyUtils.isNotEmpty(noHashValueJsonList)) {
            for (NoHashValueJson noHashValueJson : noHashValueJsonList) {
                Gson gson = new GsonBuilder()
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
                        .create();
                String recordJson = noHashValueJson.getRecordJson();
                HashIndex hashIndex1 = new HashIndex();
                hashIndex1.setBlockNum(noHashValueJson.getBlockNum());
                hashIndex1.setHashValue(noHashValueJson.getHashValue());
                hashIndex1.setRootHashValue(noHashValueJson.getRootHashValue());
                hashIndex1.setTxId(noHashValueJson.getTxId());
                hashIndex1.setTxTimeStamp(noHashValueJson.getTxTimeStamp());
                hashIndex1.setUserName(noHashValueJson.getUserName());
                hashIndexList.add(hashIndex1);
                noHashValueJsonHashValueList.add(noHashValueJson.getHashValue());
                if ("jzt_logistics".equals(noHashValueJsonList.get(0).getHashTableName())) {
                    JZTLogistics jztLogistics = gson.fromJson(recordJson, JZTLogistics.class);
                    jztLogistics.setHashValue(noHashValueJson.getHashValue());
                    jztLogistics.setRootHashValue(noHashValueJson.getRootHashValue());
                    jztLogisticsList.add(jztLogistics);
                }else if ("jzt_attachment_businesss".equals(noHashValueJsonList.get(0).getHashTableName())) {
                    JZTAttachmentBusiness jztAttachmentBusiness = gson.fromJson(recordJson, JZTAttachmentBusiness.class);
                    jztAttachmentBusiness.setHashValue(noHashValueJson.getHashValue());
                    jztAttachmentBusiness.setRootHashValue(noHashValueJson.getRootHashValue());
                    jztAttachmentBusinessList.add(jztAttachmentBusiness);
                }else if ("jzt_code_circulation".equals(noHashValueJsonList.get(0).getHashTableName())) {
                    JZTCodeCirculation jztCodeCirculation=gson.fromJson(recordJson,JZTCodeCirculation.class);
                    jztCodeCirculation.setHashValue(noHashValueJson.getHashValue());
                    jztCodeCirculation.setRootHashValue(noHashValueJson.getRootHashValue());
                    jztCodeCirculationList.add(jztCodeCirculation);
                }else if ("jzt_code_circulation_attachment".equals(noHashValueJsonList.get(0).getHashTableName())) {
                    JZTCodeCirculationAttachment jztCodeCirculationAttachment=gson.fromJson(recordJson,JZTCodeCirculationAttachment.class);
                    jztCodeCirculationAttachment.setHashValue(noHashValueJson.getHashValue());
                    jztCodeCirculationAttachment.setRootHashValue(noHashValueJson.getRootHashValue());
                    jztCodeCirculationAttachmentList.add(jztCodeCirculationAttachment);
                }else if ("jzt_GS1".equals(noHashValueJsonList.get(0).getHashTableName())) {
                    JZTGS1 jztgs1=gson.fromJson(recordJson,JZTGS1.class);

                    String userName1 = jztgs1.getUserName();
                    Date arrivalTime = jztgs1.getArrivalTime();
                    String billType=jztgs1.getBillType();
                    String billTypeName=jztgs1.getBillTypeName();
                    Date createTime=jztgs1.getCreateTime();
                    String departureAddress=jztgs1.getDepartureAddress();
                    String fromCorpName=jztgs1.getFromCorpName();
                    String hashValue=jztgs1.getHashValue();
                    String operator=jztgs1.getOperator();
                    Integer id=jztgs1.getId();
                    Date operatorTime=jztgs1.getOperatorTime();
                    String rootHashValue=jztgs1.getRootHashValue();
                    String ssccCode=jztgs1.getSsccCode();
                    String toCorpName=jztgs1.getToCorpName();
                    Date storeOutDate=jztgs1.getStoreOutDate();
                    String[] subSsccCode=jztgs1.getSubSsccCode();
                    String tprtMode=jztgs1.getTprtMode();
                    String transportMode=jztgs1.getTransportMode();
                    String transportSsa=jztgs1.getTransportSsa();
                    Date transportTime=jztgs1.getTransportTime();
                    Date updateTime=jztgs1.getUpdateTime();

                    String subSsccCodeStr= StringUtils.join(subSsccCode,",");

                    JZTGS1Str jztgs1Str=new JZTGS1Str();
                    jztgs1Str.setId(id);
                    jztgs1Str.setUserName(userName1);
                    jztgs1Str.setArrivalTime(arrivalTime);
                    jztgs1Str.setBillType(billType);
                    jztgs1Str.setBillTypeName(billTypeName);
                    jztgs1Str.setCreateTime(createTime);
                    jztgs1Str.setDepartureAddress(departureAddress);
                    jztgs1Str.setFromCorpName(fromCorpName);
                    jztgs1Str.setHashValue(hashValue);
                    jztgs1Str.setOperator(operator);
                    jztgs1Str.setOperatorTime(operatorTime);
                    jztgs1Str.setRootHashValue(rootHashValue);
                    jztgs1Str.setSsccCode(ssccCode);
                    jztgs1Str.setStoreOutDate(storeOutDate);
                    jztgs1Str.setSubSsccCode(subSsccCodeStr);
                    jztgs1Str.setToCorpName(toCorpName);
                    jztgs1Str.setTprtMode(tprtMode);
                    jztgs1Str.setTransportMode(transportMode);
                    jztgs1Str.setTransportSsa(transportSsa);
                    jztgs1Str.setTransportTime(transportTime);
                    jztgs1Str.setUpdateTime(updateTime);
                    jztgs1Str.setHashValue(noHashValueJson.getHashValue());
                    jztgs1Str.setRootHashValue(noHashValueJson.getRootHashValue());
                    jztgs1List.add(jztgs1Str);
                }else if ("jzt_GS1_attachment".equals(noHashValueJsonList.get(0).getHashTableName())) {
                    JZTGS1Attachment jztgs1Attachment=gson.fromJson(recordJson,JZTGS1Attachment.class);
                    jztgs1Attachment.setHashValue(noHashValueJson.getHashValue());
                    jztgs1Attachment.setRootHashValue(noHashValueJson.getRootHashValue());
                    jztgs1AttachmentList.add(jztgs1Attachment);
                }else if("baiwan_test".equals(noHashValueJsonList.get(0).getHashTableName())){
                    BaiWanTest baiWanTest=gson.fromJson(recordJson,BaiWanTest.class);
                    baiWanTest.setHashValue(noHashValueJson.getHashValue());
                    baiWanTest.setRootHashValue(noHashValueJson.getRootHashValue());
                    baiWanTestList.add(baiWanTest);
                }else if("wx_account_firm_verification".equals(noHashValueJsonList.get(0).getHashTableName())){
                    WxAccountFirmVerification wxAccountFirmVerification = gson.fromJson(recordJson,WxAccountFirmVerification.class);
                    wxAccountFirmVerification.setHashValue(noHashValueJson.getHashValue());
                    wxAccountFirmVerification.setRootHashValue(noHashValueJson.getRootHashValue());
                    wxAccountFirmVerificationList.add(wxAccountFirmVerification);
                }
            }
        }
        if(jztLogisticsList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordJZTLogisticsBatch(noHashValueJsonHashValueList, hashIndexList, jztLogisticsList);
        }

        if(jztAttachmentBusinessList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordJZTAttachmentBusinessBatch(noHashValueJsonHashValueList, hashIndexList, jztAttachmentBusinessList);
        }

        if(jztCodeCirculationList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordJZTCodeCirculationBatch(noHashValueJsonHashValueList, hashIndexList, jztCodeCirculationList);
        }

        if(jztCodeCirculationAttachmentList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordJZTCodeCirculationAttachmentBatch(noHashValueJsonHashValueList, hashIndexList, jztCodeCirculationAttachmentList);
        }

        if(jztgs1List.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordJZTGS1Batch(noHashValueJsonHashValueList, hashIndexList, jztgs1List);
        }

        if(jztgs1AttachmentList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordJZTGS1AttachmentBatch(noHashValueJsonHashValueList, hashIndexList, jztgs1AttachmentList);
        }

        if(baiWanTestList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordBaiWanTestBatch(noHashValueJsonHashValueList,hashIndexList,baiWanTestList);
        }

        if(wxAccountFirmVerificationList.size()>0){
            mysqlDataBaseAndMongDbConfigService.recordWxAccountFirmVerificationBatch(noHashValueJsonHashValueList,hashIndexList,wxAccountFirmVerificationList);
        }

        System.out.println("迁移上链信息定时任务结束时间：" + dateFormat.format(new Date()));
    }
}